package com.berg.metroarrived;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.berg.metroarrived.StationDB.StationsColumns;

public class StationInfo extends Activity {
	private static final String DTAG = "stationinfo"; 
	private long _ID = -1;
	private String name = null;
	private long cellId = -1;
	private boolean enabled = false;
	
	private TextView editName = null;
	private TextView editCellId = null;
	private Switch swEnabled = null;
	
	private MobileCellInfo mCellInfo;
	//private boolean dirty = false;
	
	private ContentResolver mCR = null;
	private Intent mIntent = null;
	private Uri mUri = null;
	
	
	 /**
     * The columns we are interested in from the database
     */
    private final static String[] PROJECTION = new String[] {
    	StationsColumns._ID, // 0
    	StationsColumns.NAME, // 1
    	StationsColumns.CELLID, // 2
    	StationsColumns.ENABLED, // 2
    };
    

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_station_info);
		
		// Show the Up button in the action bar.
		setupActionBar();
		
      
		editName = (TextView)findViewById(R.id.editName);
		editCellId = (TextView)findViewById(R.id.editCellId);
		swEnabled = (Switch)findViewById(R.id.bnEnabled);
		
		mCR = this.getContentResolver();
		mIntent = getIntent();
		mUri = mIntent.getData();
		
		String action = mIntent.getAction();
		Log.d(DTAG,"intent is:" + mIntent.toString());
		if(action == Intent.ACTION_VIEW)
			ViewStation();
		else if(action == Intent.ACTION_INSERT)
			addStation();
		else if(action == Intent.ACTION_EDIT)
			ViewStation();
		
		mCellInfo = new MobileCellInfo(this);

	}
	
	@Override
	protected void onPause() {
		super.onPause();
		
		//if(dirty){
			SaveDB();
		//	dirty = false;
		//}
		
	}
	

	

	private void ViewStation(){
		Log.d(DTAG,"uri is:" + mUri.toString());
		Cursor c = mCR.query(mUri, PROJECTION, null, null, null);
		Log.d(DTAG,"resutl cursor count is:" + c.getCount());
		Log.d(DTAG,"cursor  is:" + c.toString());
		if(c.getCount() == 1){
			c.moveToFirst();
			
			_ID = c.getLong(c.getColumnIndex(StationsColumns._ID));
			name = c.getString(c.getColumnIndex(StationsColumns.NAME));
			cellId = c.getLong(c.getColumnIndex(StationsColumns.CELLID));
			enabled = c.getLong(c.getColumnIndex(StationsColumns.ENABLED)) == 1?true:false;
			Display();
			//Log.d(DTAG,"cur station enabled = " + c.getString(c.getColumnIndex(StationsColumns.ENABLED)) + " " + enabled);
		}
	}
	
	private void editStation(){
		
	}

	
	private void addStation(){
		
	}

	/**
	 * Set up the {@link android.app.ActionBar}.
	 */
	private void setupActionBar() {

		getActionBar().setDisplayHomeAsUpEnabled(true);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.station_info, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	public void onCheckCellInfo(View v){
		mCellInfo.Update();
		int cellid = mCellInfo.cellId;
		editCellId.setText(String.valueOf(cellid));
		Toast.makeText(this, "cell id is " + cellid + " here", Toast.LENGTH_SHORT).show();
		//dirty = true;
		
	}
	
//	public void onResetCellInfo(View v){
//		Display();
//		dirty = false;
//	}

	private void Display(){
		editName.setText(name);
		editCellId.setText(String.valueOf(cellId));
		swEnabled.setChecked(enabled);
	}
	
	private void SaveDB(){
		ContentValues values = new ContentValues();
		String _name =  editName.getText().toString();
		String _cellId =  editCellId.getText().toString();
		String _enabled =  swEnabled.isChecked()?"1":"0";
		 
		if(_name == null || _name.isEmpty()){
			Toast.makeText(this, "the station info couldn't been saved for empty name", Toast.LENGTH_SHORT).show();
			return;
		}
		
		values.put(StationsColumns.NAME, _name);
		values.put(StationsColumns.CELLID, _cellId);
		values.put(StationsColumns.ENABLED,_enabled);
		
		Log.d(DTAG,"_ID = " + _ID + " values :" +values.toString());
		if(_ID >=0 ){
			//update
			mCR.update(mUri,values,null,null);
		}else{
			//insert
			Uri new_uri = mCR.insert(StationsColumns.CONTENT_URI,values);
			Log.d(DTAG,"inserted uri is:" + new_uri.toString());
		}
	}
}
